﻿#include "mainView.h"

PIMAGE drone = newimage();
WindowParams params;

void drawMainView()
{
	// 获取屏幕宽高
	int winW = params.width;
	int winH = params.height;
	// 以屏幕中心为中心点	
	//std::cout << "Current working dir: " << _getcwd(NULL, 0) << std::endl;
	drawDrone(winW / 2.0f, winH * 3 / 4.0f, 0.6f);
	// 将1000-2000范围转换为-90到90范围
	double realPitch = (droneData.pitch - 1500) * 0.06;
	double realRoll = (droneData.roll - 1500) * 0.18 * M_PI / 180;
	double realTh = (droneData.throttle - 1000) * (260.0 / 350.0) * M_PI / 180;
	double realYaw = (droneData.yaw - 1000) * (260.0 / 1000.0) * M_PI / 180;

	drawAT(winW  / 2.0f, winH / 5.0f, winH / 4.5f, realPitch, realRoll);

	drawPHI(winW * 5 / 6.0f, winH  / 5.0f, winH / 6.0f, realTh);

	drawYaw(winW  / 6.0f, winH / 5.0f, winH / 6.0f, realYaw);
	// 打印四个信息
	printData();

}

void printData() {
	setfont(50, 0, "Tahoma");
	char yaw_info[64], pitch_info[64], roll_info[64], throttle_info[64], ip_info[64], port_info[64];
	sprintf_s(pitch_info, "Pitch: %.1f", droneData.pitch);
	sprintf_s(roll_info, "Roll: %.1f", droneData.roll);
	sprintf_s(yaw_info, "Yaw: %.1f", droneData.yaw);
	sprintf_s(throttle_info, "Throttle: %.1f", droneData.throttle);
    sprintf_s(ip_info, "IP: %s", droneData.serverIP.c_str());
    sprintf_s(port_info, "serverPort: %d   listenPort:8081", droneData.serverPort);
	setcolor(EGERGB(0, 0, 0));
	if (droneData.isConnected)
	{
		outtextxy(846, 465, pitch_info);
		outtextxy(856, 515, roll_info);
		outtextxy(207, 485, yaw_info);
		outtextxy(1461, 485, throttle_info);
		outtextxy(50, 1100, ip_info);
		outtextxy(50, 1150, port_info);
	}
	else {
        outtextxy(50, 10, "Disconnected");
	}
	if (droneData.modeControl) {
		//outtextxy(50, 310, "Controling");
	}
	else
	{
		outtextxy(50, 310, "Receiving");
	}
}

void initMainView()
{
	SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
	setcaption("BeagleBone");

	//params.width = GetSystemMetrics(SM_CXSCREEN);
	//params.height = GetSystemMetrics(SM_CYSCREEN);
    params.width = 1920;
    params.height = 1200;

	initgraph(static_cast<int>(params.width), static_cast<int>(params.height), INIT_NOFORCEEXIT /*| INIT_NOBORDER*/);
	setbkcolor(WHITE);
}

void drawDrone(float x0, float y0, float BackScale)
{
	getimage_pngfile(drone, "resources/DJI_Mini.png");
	int imgW = getwidth(drone);
	int imgH = getheight(drone);
	float drawW = imgW * BackScale;
	float drawH = imgH * BackScale;
	float left = x0 - drawW / 2.0f;
	float top = y0 - drawH / 2.0f;
	ege_drawimage(drone, left, top, drawW, drawH, 0, 0, imgW, imgH, NULL);
}

void drawPanel(float x0, float y0, float BackScale) 
{
	
}

void closeMainView() {
	delimage(drone);
}

